package com.smarthospital.common.Result;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

/**
 * create by sym  2021/3/22 18:52.
 */
@Data
@Slf4j
@ApiModel(description="codeMsg封装" )
public class ResultDTO<T> {
    @ApiModelProperty(value = "code",example="0",allowableValues="500_系统繁忙 403_权限不足")
    private Integer code;
    @ApiModelProperty(value = "code",example="访问成功")
    private String msg;
    @ApiModelProperty(value = "返回对象")
    private T data;

    public ResultDTO(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public ResultDTO(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }
    public ResultDTO(CodeMsg codeMsg) {
        this.code = codeMsg.getCode();
        this.msg = codeMsg.getMsg();
    }
    public static <T>ResultDTO<T> sucess(T data){
        ResultDTO sucess = sucess();
        sucess.setData(data);
        return sucess;
    }

    public static ResultDTO sucess(){
        return new ResultDTO(CodeMsg.SUCESS);
    }

    public static ResultDTO fail(){
        return new ResultDTO(CodeMsg.FAIL);
    }

    public static ResultDTO fail(CommonException c){
        return new ResultDTO(c.getCode(),c.getMsg());
    }
    public static ResultDTO fail(CodeMsg c,String msg){
        return new ResultDTO(c.getCode(),msg);
    }

}
